// ActionScript file
package com.kinderas.net{
	import flash.display.LoaderInfo;
	import flash.external.ExternalInterface;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.system.LoaderContext;
	/**
	 * The Browser class adds JavaScript like functionality to
	 * your Flash/Flex project.
	 * @author Jørn Kinderås
	 * 
	 */	
	public class Browser{
		
		/**
		 * The getUrlParams method returns an Array of GET id's taken
		 * from the browsers address line. The Array is indexed by id, so
		 * if you want an id with name <i>name</i> you go myArray['name'];
		 * <p>Usage:</p>
		 * <listing>
		 * var myArray:Array = Browser.getUrlParams();
		 * trace(myArray['id']);
		 * </listing>
		 * Input ------> http://url.com?id=123&amp;name=ole
		 * Outputs ----> 123
		 * @return Indexed array containing url parameters
		 * 
		 */		
		public static function getUrlParams():Array{
			if(!ExternalInterface.available){
				throw new Error("ExternalInterface is not available, you'll need to allowScriptAccess");
				return;
			}
			var myStr:String = ExternalInterface.call("window.location.search.substring", 1);
			var params:Array = myStr.split("&");
			var output:Array = new Array();
			for(var i:int=0;i<params.length;i++){
				var buffer:Array = params[i].split("=");
				output[buffer[0]] = buffer[1];
			}
			return output;
		}
		/**
		 * The openURL function allows you to open a browser Window, much like the navigateToURL does.
		 * However, this function will bypass popup blockers in Safari, Firefox and Internet Explorer
		 * 
		 * @param The url you wish to open
		 * @param State of new window(default is _blank)
		 * 
		 */		
		public static function openURL(url:String, window:String = "_blank") : void {
			var WINDOW_OPEN_FUNCTION:String = "window.open";
			if(url == null || url == ''){
				throw new Error("The url parameter is required!");
			}
            if(!ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window)){
            	navigateToURL(new URLRequest(url),window);
            }
        }
	}//End Class
}//End package